//
//  StoreColCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/8.
//

import UIKit

class StoreColCell: UICollectionViewCell {
    
    lazy var getCount: UILabel = {
        let label = UILabel()
        label.textColor = .black
        label.font = .wn_font(name: .bold, size: 18)
        return label
    }()
    
    lazy var getGold: UILabel = {
        let label = UILabel()
        label.text = "金幣"
        label.textColor = .black
        label.font = .wn_font(name: .bold, size: 12)
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView()
    }()
    
    lazy var giveCount: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "gray_999999")
        label.font = .wn_font(name: .bold, size: 14)
        return label
    }()
    
    lazy var giveGold: UILabel = {
        let label = UILabel()
        label.text = "金幣"
        label.textColor = .black
        label.font = .wn_font(name: .bold, size: 12)
        return label
    }()
    
    lazy var giveDiscount: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = .init(named: "red_FE6390")
        label.font = .wn_font(name: .bold, size: 9)
        label.wn_cornerWithBorder(8, 0.5, .init(named: "red_FE95B4")!)
        return label
    }()
            
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    lazy var priceLabel: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "red_FE6390")
        label.font = .wn_font(name: .bold, size: 18)
        label.textAlignment = .center
        return label
    }()
    
    lazy var buyBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.isHidden = true
        return btn
    }()
        
    var model: RechargeDetailModel? {
        didSet {
            if let m = model {
                getCount.text = "\(m.gold)"
                giveCount.text = "+\(m.goldGive)"
                giveDiscount.text = "+\(m.disGold)%"
                priceLabel.text = "USD \(m.price)"
                goldImage.image = UIImage(named: "coin_\(tag)")
                if m.goldGive == 0 {
                    getCount.snp.remakeConstraints { (make) in
                        make.centerX.equalToSuperview().offset(-22)
                        make.centerY.equalToSuperview().offset(-32)
                    }
                }else {
                    getCount.snp.remakeConstraints { (make) in
                        make.top.equalTo(14)
                        make.centerX.equalToSuperview().offset(-22)
                    }
                }
                giveCount.isHidden = m.goldGive == 0
                giveGold.isHidden = m.goldGive == 0
                giveDiscount.isHidden = m.goldGive == 0
                layoutIfNeeded()
            }
        }
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(getCount)
        addSubview(getGold)
        addSubview(goldImage)
        addSubview(giveGold)
        addSubview(giveCount)
        addSubview(giveDiscount)
        addSubview(lineView)
        addSubview(priceLabel)
        addSubview(buyBtn)
        getCount.snp.makeConstraints { (make) in
            make.top.equalTo(14)
            make.centerX.equalToSuperview().offset(-22)
        }
        getGold.snp.makeConstraints { (make) in
            make.bottom.equalTo(getCount.snp.bottom).offset(-2)
            make.left.equalTo(getCount.snp.right).offset(4)
        }
        goldImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(getCount.snp.bottom).offset(0)
            make.width.height.equalTo(40)
        }
        giveGold.snp.makeConstraints { (make) in
            make.top.equalTo(goldImage.snp.bottom).offset(0)
            make.centerX.equalToSuperview()
        }
        giveCount.snp.makeConstraints { (make) in
            make.centerY.equalTo(giveGold.snp.centerY)
            make.right.equalTo(giveGold.snp.left).offset(-6)
        }
        giveDiscount.snp.makeConstraints { (make) in
            make.centerY.equalTo(giveGold.snp.centerY)
            make.left.equalTo(giveGold.snp.right).offset(6)
            make.width.equalTo(50)
            make.height.equalTo(16)
        }
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(-scaleWidth(34))
            make.height.equalTo(0.5)
        }
        priceLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom).offset(scaleWidth(5))
        }
        buyBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        wn_cornerWithBorder(10, 0, .white, 1, .init(white: 0, alpha: 0.1), .init(width: 0, height: 0), 10)
        layer.masksToBounds = false
    }
    
    func darkModeConfig() {
        buyBtn.isHidden = false
        backgroundColor = isDark() ? .init(named: "red_A15F73") : .init(named: "white_FBF6F1")
        getCount.textColor = isDark() ? .white : .black
        getGold.textColor = isDark() ? .white : .black
        giveCount.textColor = isDark() ? .init(named: "red_D0AFB9") : .init(named: "gray_999999")
        giveGold.textColor = isDark() ? .white : .black
        lineView.backgroundColor = isDark() ? .init(named: "gray_999999") : .init(named: "gray_D0D0D0")
        giveDiscount.textColor = isDark() ? .init(named: "red_FFB8C7") : .init(named: "red_FE6390")
        giveDiscount.wn_cornerWithBorder(8, 0.5, isDark() ? .init(named: "red_D0AFB9")! : .init(named: "red_FE95B4")!)
        priceLabel.textColor = isDark() ? .init(named: "red_FFB8C7") : .init(named: "red_FE6390")
        wn_cornerCustom([.allCorners], 10)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
